import { IsString, IsEnum, IsOptional, IsInt, IsArray, Min, Max } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { MemoryType } from '@prisma/client';
import { Transform } from 'class-transformer';

// Create memory DTO
export class CreateMemoryDto {
  @ApiProperty({ description: 'Memory content' })
  @IsString()
  content: string;

  @ApiProperty({ description: 'Memory type', enum: MemoryType, required: false })
  @IsOptional()
  @IsEnum(MemoryType)
  type?: MemoryType;

  @ApiProperty({ description: 'Importance level (1-5)', minimum: 1, maximum: 5, required: false })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(5)
  importance?: number;

  @ApiProperty({ description: 'Tags', type: [String], required: false })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[];
}

// Update memory DTO
export class UpdateMemoryDto {
  @ApiProperty({ description: 'Memory content', required: false })
  @IsOptional()
  @IsString()
  content?: string;

  @ApiProperty({ description: 'Memory type', enum: MemoryType, required: false })
  @IsOptional()
  @IsEnum(MemoryType)
  type?: MemoryType;

  @ApiProperty({ description: 'Importance level (1-5)', minimum: 1, maximum: 5, required: false })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(5)
  importance?: number;

  @ApiProperty({ description: 'Tags', type: [String], required: false })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[];
}

// Memory query DTO
export class MemoryQueryDto {
  @ApiProperty({ description: 'Memory type', enum: MemoryType, required: false })
  @IsOptional()
  @IsEnum(MemoryType)
  type?: MemoryType;

  @ApiProperty({ description: 'Tag filter', type: [String], required: false })
  @IsOptional()
  @Transform(({ value }) => {
    if (typeof value === 'string') {
      return value.split(',').map(tag => tag.trim()).filter(tag => tag.length > 0);
    }
    return value;
  })
  @IsArray()
  @IsString({ each: true })
  tags?: string[];

  @ApiProperty({ description: 'Limit count', minimum: 1, maximum: 100, required: false })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  limit?: number;

  @ApiProperty({ description: 'Offset', minimum: 0, required: false })
  @IsOptional()
  @IsInt()
  @Min(0)
  offset?: number;
}
